package wang.zl.bookmanager1.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import wang.zl.bookmanager1.R.ResultInfo;
import wang.zl.bookmanager1.domain.Book_type;
import wang.zl.bookmanager1.domain.User;
import wang.zl.bookmanager1.service.impl.Book_typeServiceImpl;

import java.lang.reflect.Type;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/book_type")
public class Book_typeController {
    @Autowired
    Book_typeServiceImpl bookTypeService;
    @PostMapping("getAll")
    public ResultInfo getAll(){
        LambdaQueryWrapper<Book_type> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc();
        List<Book_type> list = bookTypeService.list(qw);
        return new ResultInfo(true,list,"成功");
    }
    @PostMapping("AddType")
    @Transactional
    public ResultInfo add(String typeName,String remark){
        Book_type book_type = new Book_type();
        book_type.setTypeName(typeName);
        book_type.setRemark(remark);
        bookTypeService.save(book_type);

        return new ResultInfo(true,null,"添加成功");
    }
    @PostMapping("Update")
    public ResultInfo update(Book_type type,Long typeId ){

        LambdaQueryWrapper<Book_type> qw1 = new LambdaQueryWrapper<>();
        if(!typeId.equals(type.getId())){
            LambdaQueryWrapper<Book_type> qw = new LambdaQueryWrapper<>();
            qw.eq(Book_type::getId,type.getId());
            Book_type one = bookTypeService.getOne(qw);

            if(one != null){
                return new ResultInfo(false,null,"编号已经存在了,请换一个");
            }else {

                qw1.eq(Book_type::getTypeName,type.getTypeName());
                Book_type one1 = bookTypeService.getOne(qw);
                if (one1 == null) {
                    bookTypeService.removeById(typeId);
                    bookTypeService.save(type);
                    return new ResultInfo(true, null, "修改成功");
                }else {
                    return new ResultInfo(false,null,"名称有相同的");
                }
            }
        }else {
            bookTypeService.removeById(typeId);

            qw1.eq(Book_type::getTypeName,type.getTypeName());
            Book_type one1 = bookTypeService.getOne(qw1);
            if (one1 == null) {
                bookTypeService.save(type);
                return new ResultInfo(true, null, "修改成功");
            }else {
                return new ResultInfo(false,null,"名称有相同的");
            }

        }
    }
    @PostMapping("remove")
    public ResultInfo remove(String TypeId){

        if (bookTypeService.removeById(TypeId)) {
            return new ResultInfo(true,null,"删除成功");
}else {
    return new ResultInfo(false,null,"删除错误");
}



    }
}

